/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.iussoft.portal.tags
 */
package com.apache.portal.tags;

import java.util.Date;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.httpclient.util.DateUtil;

import com.apache.tools.DateUtils;
import com.apache.tools.DateUtils.FmtStr;

/**
 * description:  获了map值
 * @author Hou Dayu 创建时间：2016-8-29
 */
@SuppressWarnings("serial")
public class FindMapTag extends TagSupport {

	private Object items;
	private String key;
	private String isDate = "false";
	private String format = "yyyy-MM-dd HH:mm:ss";

	//private JspWriter writer;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int doStartTag() throws JspException {
		try {
			Map<String, Object> map = (Map) items;
			JspWriter writer = this.pageContext.getOut();// 返回一个JspWriter
			Object obj = map.get(key);
			String value = "";
			if (null != obj) {
				if (obj instanceof java.sql.Timestamp) {
					value = DateUtil.formatDate((Date) obj, format);
				} else if (obj instanceof java.lang.Long) {
					if ("true".equals(isDate)) {
						value = DateUtils.longToStr((Long) obj, FmtStr.CN_yyyyMMdd_HHmmss);
					} else {
						value = String.valueOf(obj);
					}
				} else {
					value = String.valueOf(obj.toString());
				}
			}
			writer.print(value);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.EVAL_BODY_INCLUDE;
	}

	//	public int doEndTag() {
	//		try {
	//			if (null != writer)
	//				writer.close();
	//		} catch (Exception e) {
	//		}
	//		return super.EVAL_BODY_INCLUDE;
	//	}

	public Object getItems() {
		return items;
	}

	public void setItems(Object items) {
		this.items = items;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getIsDate() {
		return isDate;
	}

	public void setIsDate(String isDate) {
		this.isDate = isDate;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

}
